<?php
class Action
{
	function __call($id = 0, $ad = array())
	{
		if ($id = intval($id)) {
		  	if (!$data_t = _model('category')->read(array('id'=>$id))) {
    		  die('error');
    		}
		}

		$return = array();

		if($id == 0){
			$stats_a = 'm';
		} elseif($s_data = _model('category')->read(array('parent_id'=>$id))){
			$stats_a = 'a';
		} else {
			$stats_a = 'l';
		}
		
		$info = _model('setting')->getList('ORDER BY id ASC');
		$return = array('id'=>$id, 'type'=>$stats_a, 'icon'=>SITE_URL.'/Services/images/cropped_512_384/attachments'.$info[1]['value'], 'bg'=>SITE_URL.'/Services/images/cropped_1024_768/attachments'.$info[1]['value'], 'title'=>$info[0]['value']);

		switch($stats_a){
			case 'l':
				$data = _model('item')->getList(array('category_id'=>$id));
				foreach($data as $val){
					$return['entities'][] = array('id'=>$val['id'], 'icon'=>SITE_URL.'/Services/images/cropped_160_206/attachments'.$val['thum_pic'], 'title'=>'', 'type'=>'p', 'bg'=>SITE_URL.'/attachments'.$val['url']);
				}
			break;
			case 'm':
				$data = _model('category')->getList(array('parent_id'=>$id));
				foreach($data as $val){
					$stats = _model('category')->read(array('parent_id'=>$val['id']));
					if(!empty($stats)){
						$type = 'a';
					} else {
						$type = 'l';
					}
					$return['entities'][] = array('id'=>$val['id'], 'icon'=>SITE_URL.'/Services/images/cropped_160_145/attachments'.$val['img'], 'title'=>$val['title'], 'type'=>$type, 'bg'=>SITE_URL.'/attachments'.$val['img']);
				}
				$return['entities'][] = array('id'=> -1, 'icon'=>SITE_URL.'/Services/images/cropped_160_145/attachments'.$val['img']);
			break;
			default :
				$data = _model('category')->getList(array('parent_id'=>$id));
				foreach($data as $val){
					$stats = _model('category')->read(array('parent_id'=>$val['id']));
					if(!empty($stats)){
						$type = 'a';
					} else {
						$type = 'l';
					}
					$return['entities'][] = array('id'=>$val['id'], 'icon'=>SITE_URL.'/Services/images/cropped_176_226/attachments'.$val['img'], 'title'=>$val['title'], 'type'=>$type, 'bg'=>SITE_URL.'/attachments'.$val['img']);
				}
			break;
		}
		
		echo json_encode($return);

	}
}
?>